package com.yu.servlet;

import com.yu.Pojo.User;
import com.yu.service.UserService;
import com.yu.serviceImpl.UserServiceImpl;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Service;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String userCode = req.getParameter("userCode");
        String userName = req.getParameter("userName");
        User user = new User();
        user.setUserCode (userCode);
        user.setUserName (userName);
        System.out.println (user);

        UserService userService = new UserServiceImpl ();

        try {
            User user2 = userService.findUser (user);
            if (user2 != null){
                //把用户信息存在session中
                req.getSession ().setAttribute ("user",user2);
                System.out.println ("11111111111");
                //String rs = "s!";
                String str = "{msg:'success',status:'200'}";
                PrintWriter outs = resp.getWriter();
                outs.print(str);
            }

            else{
                System.out.println ("000000");
                String rs = "名字重复，失败!";
                PrintWriter outs = resp.getWriter();
                outs.print(rs);

            }

        } catch (SQLException throwables) {
            throwables.printStackTrace ();
        } catch (ClassNotFoundException e) {
            e.printStackTrace ();
        }


    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
